package com.sls.game.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sls.core.http.HttpClient;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.game.dao.NutsNetGoodsInfoDAO;
import com.sls.game.dao.nutsnet.AccountTypeDAO;
import com.sls.game.dao.nutsnet.ChargeTypeInfoDAO;
import com.sls.game.dao.nutsnet.ChooseGemDAO;
import com.sls.game.dao.nutsnet.NutsNetGoodsAreaDAO;
import com.sls.game.dao.nutsnet.NutsNetGoodsDetailTopDAO;
import com.sls.game.dao.nutsnet.NutsNetGoodsServerDAO;
import com.sls.game.parser.nutsnet.NutsNetResponseDomParseXml;
import com.sls.game.parser.nutsnet.NutsNetServerSaxParseXml;
import com.sls.game.pojo.NutsNetGoodsInfo;
import com.sls.game.pojo.NutsNetServerInfo;
import com.sls.game.pojo.nutsnet.AccountTypeInfo;
import com.sls.game.pojo.nutsnet.ChargeTypeInfo;
import com.sls.game.pojo.nutsnet.ChooseGemInfo;
import com.sls.game.pojo.nutsnet.NutsNetGoodsAreaInfo;
import com.sls.game.pojo.nutsnet.NutsNetGoodsDetailTop;
import com.sls.game.pojo.nutsnet.NutsNetGoodsServerInfo;
import com.sls.game.service.NutsNetGoodsDetailXMLService;

/**
 * @Description:坚果网络商品详细XML
 * @author Wangxuegang
 * @time 2012-12-06
 */

@Service("nutsNetGoodsDetailXMLService")
public class NutsNetGoodsDetailXMLServiceImpl implements
		NutsNetGoodsDetailXMLService {

	private static Logger log = Logger
			.getLogger(NutsNetGoodsAddServiceImpl.class);

	private NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO;

	private ChargeTypeInfoDAO chargeTypeInfoDAO;

	private NutsNetGoodsAreaDAO nutsNetGoodsAreaDAO;

	private NutsNetGoodsServerDAO nutsNetGoodsServerDAO;

	private AccountTypeDAO accountTypeDAO;

	private ChooseGemDAO chooseGemDAO;

	private NutsNetGoodsDetailTopDAO nutsNetGoodsDetailTopDAO;

	public NutsNetGoodsDetailTopDAO getNutsNetGoodsDetailTopDAO() {
		return nutsNetGoodsDetailTopDAO;
	}

	@Resource(name = "nutsNetGoodsDetailTopDAO")
	public void setNutsNetGoodsDetailTopDAO(
			NutsNetGoodsDetailTopDAO nutsNetGoodsDetailTopDAO) {
		this.nutsNetGoodsDetailTopDAO = nutsNetGoodsDetailTopDAO;
	}

	public ChooseGemDAO getChooseGemDAO() {
		return chooseGemDAO;
	}

	@Resource(name = "chooseGemDAO")
	public void setChooseGemDAO(ChooseGemDAO chooseGemDAO) {
		this.chooseGemDAO = chooseGemDAO;
	}

	public AccountTypeDAO getAccountTypeDAO() {
		return accountTypeDAO;
	}

	@Resource(name = "accountTypeDAO")
	public void setAccountTypeDAO(AccountTypeDAO accountTypeDAO) {
		this.accountTypeDAO = accountTypeDAO;
	}

	public NutsNetGoodsServerDAO getNutsNetGoodsServerDAO() {
		return nutsNetGoodsServerDAO;
	}

	@Resource(name = "nutsNetGoodsServerDAO")
	public void setNutsNetGoodsServerDAO(
			NutsNetGoodsServerDAO nutsNetGoodsServerDAO) {
		this.nutsNetGoodsServerDAO = nutsNetGoodsServerDAO;
	}

	public NutsNetGoodsAreaDAO getNutsNetGoodsAreaDAO() {
		return nutsNetGoodsAreaDAO;
	}

	@Resource(name = "nutsNetGoodsAreaDAO")
	public void setNutsNetGoodsAreaDAO(NutsNetGoodsAreaDAO nutsNetGoodsAreaDAO) {
		this.nutsNetGoodsAreaDAO = nutsNetGoodsAreaDAO;
	}

	public ChargeTypeInfoDAO getChargeTypeInfoDAO() {
		return chargeTypeInfoDAO;
	}

	@Resource(name = "chargeTypeInfoDAO")
	public void setChargeTypeInfoDAO(ChargeTypeInfoDAO chargeTypeInfoDAO) {
		this.chargeTypeInfoDAO = chargeTypeInfoDAO;
	}

	public NutsNetGoodsInfoDAO getNutsNetGoodsInfoDAO() {
		return nutsNetGoodsInfoDAO;
	}

	@Resource(name = "nutsNetGoodsInfoDAO")
	public void setNutsNetGoodsInfoDAO(NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO) {
		this.nutsNetGoodsInfoDAO = nutsNetGoodsInfoDAO;
	}

	/**
	 * 同步所有商品详细
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String saveGoodsDetailXML(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		NutsNetGoodsInfo nutsNetGoodsInfo = new NutsNetGoodsInfo();
		nutsNetGoodsInfo.setGoodsDelete("1"); // 1:上架
		nutsNetGoodsInfo.setIsAudit("1"); // 1:已审核
		// 查询坚果网络原始商品表
		final List<NutsNetGoodsInfo> goodsIdlist = nutsNetGoodsInfoDAO
				.queryAllGoodsId(nutsNetGoodsInfo);
		new Thread(new Runnable() {
			@Override
			public void run() {
				if (goodsIdlist != null && goodsIdlist.size() > 0) {
					for (NutsNetGoodsInfo goodsInfo : goodsIdlist) {
						String goodsId = goodsInfo.getSupProductId();
						// 坚果网络商品详细访问Url
						String url = assembleNutsNetUrl(goodsId);
						sendHttpRequestOfGoodsDetail(url, goodsId);
					}
				}
			}

		}).start();

		return null;
	}

	/**
	 * 坚果网络商品详细访问Url
	 * 
	 * @param originalGoodsId
	 * @return
	 */
	private String assembleNutsNetUrl(String originalGoodsId) {
		NutsNetServerInfo nutsNetServerInfo = null;
		String serverIp = "";
		String serverPort = "";
		String filePath = "conf" + File.separator + "nutsNetServer.xml";
		List<NutsNetServerInfo> list = NutsNetServerSaxParseXml
				.parseXmlString(filePath);
		nutsNetServerInfo = list.get(0);
		if (nutsNetServerInfo != null) {
			serverIp = nutsNetServerInfo.getIp().trim();
			serverPort = nutsNetServerInfo.getPort().trim();
		}

		StringBuffer strBuf = new StringBuffer();
		strBuf.append("http://");
		strBuf.append(serverIp);
		strBuf.append(":");
		strBuf.append(serverPort);
		strBuf.append("/");
		strBuf.append("xml");
		strBuf.append("/");
		strBuf.append(originalGoodsId);
		strBuf.append(".xml");

		String url = strBuf.toString().trim();
		return url;
	}

	/**
	 * 发送http请求，并对返回的xml格式的结果进行解析
	 * 
	 * @param url
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Map<String, Object> sendHttpRequestOfGoodsDetail(String url,
			String originalGoodsId) {
		Map<String, Object> map = null;
		HttpClient httpClient = new HttpClient();
		try {
			String supplyEncode = "GBK";
			String xmlStr = httpClient.doGet(url, supplyEncode);
			map = NutsNetResponseDomParseXml.parseXmlOfGoodsDetailForTrade(
					xmlStr, originalGoodsId);
		} catch (Exception e) {
			log.error(e);
		}

		List<ChargeTypeInfo> chargetypeList = null;
		List<NutsNetGoodsAreaInfo> nutsNetGoodsAreaInfoList = null;
		List<NutsNetGoodsServerInfo> nutsNetGoodsServerInfoList = null;
		List<AccountTypeInfo> accountTypeInfoList = null;
		List<ChooseGemInfo> chooseGemInfoList = null;
		List<NutsNetGoodsDetailTop> goodsdetailTopList = null;
		if (map != null) {
			chargetypeList = (List<ChargeTypeInfo>) map.get("chargetypeList");
			nutsNetGoodsAreaInfoList = (List<NutsNetGoodsAreaInfo>) map
					.get("nutsNetGoodsAreaInfoList");
			nutsNetGoodsServerInfoList = (List<NutsNetGoodsServerInfo>) map
					.get("nutsNetGoodsServerInfoList");
			accountTypeInfoList = (List<AccountTypeInfo>) map
					.get("accountTypeInfoList");
			chooseGemInfoList = (List<ChooseGemInfo>) map
					.get("chooseGemInfoList");
			goodsdetailTopList = (List<NutsNetGoodsDetailTop>) map
					.get("goodsdetailTopList");
		}
		insertChargeType(chargetypeList);

		String chargeTypeName = gainChargeTypeName(chargetypeList);
		List<NutsNetGoodsAreaInfo> nutsNetGoodsAreaInfoList1 = new ArrayList<NutsNetGoodsAreaInfo>();
		if (nutsNetGoodsAreaInfoList != null
				&& nutsNetGoodsAreaInfoList.size() > 0) {
			for (NutsNetGoodsAreaInfo nna : nutsNetGoodsAreaInfoList) {
				nna.setChargetypeName(chargeTypeName);
				nutsNetGoodsAreaInfoList1.add(nna);
			}
			insertGameArea(nutsNetGoodsAreaInfoList1);
		}

		insertGameServer(nutsNetGoodsServerInfoList);
		insertAccountType(accountTypeInfoList);
		insertChooseGem(chooseGemInfoList);
		insertGoodsDetailTop(goodsdetailTopList);

		return map;
	}

	/**
	 * 向nutsnet_charge_type_info表中插入数据
	 * 
	 * @param chargetypeList
	 */
	private void insertChargeType(List<ChargeTypeInfo> chargetypeList) {
		if (chargetypeList != null) {
			for (ChargeTypeInfo chargeTypeInfo : chargetypeList) {
				if (chargeTypeInfo != null) {
					try {
						List<ChargeTypeInfo> list = chargeTypeInfoDAO
								.queryOneOrMoreChargeType(chargeTypeInfo);
						if (list != null && list.size() > 0) {
							for (ChargeTypeInfo typeInfo : list) {
								String id = typeInfo.getId();
								try {
									chargeTypeInfoDAO.deleteById(id);
								} catch (Exception e) {
									log.error(e);
								}
							}
							String id = UUID.randomUUID().toString();
							chargeTypeInfo.setId(id);
							try {
								chargeTypeInfoDAO.insert(chargeTypeInfo);
							} catch (Exception e) {
								log.error(e);
							}

						} else {
							String id = UUID.randomUUID().toString();
							chargeTypeInfo.setId(id);
							try {
								chargeTypeInfoDAO.insert(chargeTypeInfo);
							} catch (Exception e) {
								log.error(e);
							}
						}
					} catch (Exception e) {
						log.error(e);
					}
				}
			}
		}
	}

	/**
	 * 获取ChargeTypeName
	 * 
	 * @param chargetypeList
	 * @return
	 */
	private String gainChargeTypeName(List<ChargeTypeInfo> chargetypeList) {
		String chargeTypeName = null;
		if (chargetypeList != null && chargetypeList.size() > 0
				&& chargetypeList.size() == 1) {
			for (ChargeTypeInfo chargeTypeInfo : chargetypeList) {
				String chargeTypeNameStr = chargeTypeInfo.getChargetypeName();
				if (chargeTypeNameStr != null
						&& !("".equals(chargeTypeNameStr))) {
					chargeTypeName = chargeTypeNameStr;
				}
			}
		}
		return chargeTypeName;
	}

	/**
	 * 向nutsnet_game_area_info表中插入数据
	 * 
	 * @param nutsNetGoodsAreaInfoList
	 */
	private void insertGameArea(
			List<NutsNetGoodsAreaInfo> nutsNetGoodsAreaInfoList) {
		if (nutsNetGoodsAreaInfoList != null) {
			for (NutsNetGoodsAreaInfo nutsNetGoodsAreaInfo : nutsNetGoodsAreaInfoList) {
				if (nutsNetGoodsAreaInfo != null) {
					try {
						List<NutsNetGoodsAreaInfo> list = nutsNetGoodsAreaDAO
								.queryOneOrMoreGameArea(nutsNetGoodsAreaInfo);
						if (list != null && list.size() > 0) {
							for (NutsNetGoodsAreaInfo areaInfo : list) {
								String id = areaInfo.getId();
								try {
									nutsNetGoodsAreaDAO.deleteById(id);
								} catch (Exception e) {
									log.error(e);
								}
							}
							String id = UUID.randomUUID().toString();
							nutsNetGoodsAreaInfo.setId(id);
							try {
								nutsNetGoodsAreaDAO
										.insert(nutsNetGoodsAreaInfo);
							} catch (Exception e) {
								log.error(e);
							}
						} else {
							String id = UUID.randomUUID().toString();
							nutsNetGoodsAreaInfo.setId(id);
							try {
								nutsNetGoodsAreaDAO
										.insert(nutsNetGoodsAreaInfo);
							} catch (Exception e) {
								log.error(e);
							}
						}
					} catch (Exception e) {
						log.error(e);
					}
				}
			}
		}
	}

	/**
	 * 向nutsnet_game_server_info表中插入数据
	 * 
	 * @param nutsNetGoodsServerInfoList
	 */
	private void insertGameServer(
			List<NutsNetGoodsServerInfo> nutsNetGoodsServerInfoList) {
		if (nutsNetGoodsServerInfoList != null) {
			for (NutsNetGoodsServerInfo nutsNetGoodsServerInfo : nutsNetGoodsServerInfoList) {
				if (nutsNetGoodsServerInfo != null) {
					try {
						List<NutsNetGoodsServerInfo> list = nutsNetGoodsServerDAO
								.queryOneOrMoreGameServer(nutsNetGoodsServerInfo);
						if (list != null && list.size() > 0) {
							for (NutsNetGoodsServerInfo serInfo : list) {
								String id = serInfo.getId();
								try {
									nutsNetGoodsServerDAO.deleteById(id);
								} catch (Exception e) {
									log.error(e);
								}

							}
							String id = UUID.randomUUID().toString();
							nutsNetGoodsServerInfo.setId(id);
							try {
								nutsNetGoodsServerDAO
										.insert(nutsNetGoodsServerInfo);
							} catch (Exception e) {
								log.error(e);
							}

						} else {
							String id = UUID.randomUUID().toString();
							nutsNetGoodsServerInfo.setId(id);
							try {
								nutsNetGoodsServerDAO
										.insert(nutsNetGoodsServerInfo);
							} catch (Exception e) {
								log.error(e);
							}
						}
					} catch (Exception e) {
						log.error(e);
					}
				}
			}
		}
	}

	/**
	 * 向nutsnet_accounttype_info表中插入数据
	 * 
	 * @param accountTypeInfoList
	 */
	private void insertAccountType(List<AccountTypeInfo> accountTypeInfoList) {
		if (accountTypeInfoList != null) {
			for (AccountTypeInfo accountTypeInfo : accountTypeInfoList) {
				if (accountTypeInfo != null) {
					try {
						List<AccountTypeInfo> list = accountTypeDAO
								.queryOneOrMoreAccount(accountTypeInfo);
						if (list != null && list.size() > 0) {
							for (AccountTypeInfo accountInfo : list) {
								String id = accountInfo.getId();
								try {
									accountTypeDAO.deleteById(id);
								} catch (Exception e) {
									log.error(e);
								}
							}
							String id = UUID.randomUUID().toString();
							accountTypeInfo.setId(id);
							try {
								accountTypeDAO.insert(accountTypeInfo);
							} catch (Exception e) {
								log.error(e);
							}
						} else {
							String id = UUID.randomUUID().toString();
							accountTypeInfo.setId(id);
							try {
								accountTypeDAO.insert(accountTypeInfo);
							} catch (Exception e) {
								log.error(e);
							}
						}
					} catch (Exception e) {
						log.error(e);
					}
				}
			}
		}
	}

	/**
	 * 向nutsnet_choosegem_info表中插入数据
	 * 
	 * @param chooseGemInfoList
	 */
	private void insertChooseGem(List<ChooseGemInfo> chooseGemInfoList) {
		if (chooseGemInfoList != null) {
			for (ChooseGemInfo chooseGemInfo : chooseGemInfoList) {
				if (chooseGemInfo != null) {
					try {
						List<ChooseGemInfo> list = chooseGemDAO
								.queryOneOrMoreGift(chooseGemInfo);
						if (list != null && list.size() > 0) {
							for (ChooseGemInfo gemInfo : list) {
								String id = gemInfo.getId();
								try {
									chooseGemDAO.deleteById(id);
								} catch (Exception e) {
									log.error(e);
								}
							}
							String id = UUID.randomUUID().toString();
							chooseGemInfo.setId(id);
							try {
								chooseGemDAO.insert(chooseGemInfo);
							} catch (Exception e) {
								log.error(e);
							}
						} else {
							String id = UUID.randomUUID().toString();
							chooseGemInfo.setId(id);
							try {
								chooseGemDAO.insert(chooseGemInfo);
							} catch (Exception e) {
								log.error(e);
							}
						}
					} catch (Exception e) {
						log.error(e);
					}
				}
			}
		}
	}

	/**
	 * 向nutsnet_goods_detail_top表中插入数据
	 * 
	 * @date 2012-12-06
	 */
	private void insertGoodsDetailTop(List<NutsNetGoodsDetailTop> list) {
		if (list != null) {
			for (NutsNetGoodsDetailTop topInfo : list) {
				if (topInfo != null) {
					try {
						String goodsId = topInfo.getGoodsId();
						NutsNetGoodsDetailTop detailTop = null;
						detailTop = nutsNetGoodsDetailTopDAO.queryInfo(goodsId);
						if (detailTop != null) {
							String id = detailTop.getId();
							topInfo.setId(id);
							nutsNetGoodsDetailTopDAO.update(topInfo);
						} else {
							String id = UUID.randomUUID().toString();
							topInfo.setId(id);
							nutsNetGoodsDetailTopDAO.insert(topInfo);
						}
					} catch (Exception e) {
						log.error(e);
					}
				}
			}
		}
	}

}
